<?php

namespace app\student\controller;

use app\student\model\teacherModel;

class Teacher extends \think\Controller
{

    //查询老师
    public function selectTeacher()
    {
        $selectTeacher = teacherModel::selectTeacherModel();
        $this->assign('selectTeacher',$selectTeacher);
        return $this->fetch('teacher/selectTeacher');
    }

    //增加老师
    public function insertTeacher()
    {
        if($this->request->isPost())
        {
             $insert = $this->request->post();
             //var_dump($insert);die;
            //用来判断老师是否选了科目，这里判断老师必须选择一个科目，不然无法添加
            if(!(isset($insert['c_name'])))
            {
                //var_dump(1);die;
                return $this->success('添加失败','teacher/insertTeacher');
            }

            //时间
            $create_time = date('Y-m-d H:i:s');
            $insert['create_time'] = $create_time;
            $result = teacherModel::insertTeacherModel($insert);
            if($result)
            {
                return $this->success('添加成功','teacher/selectTeacher');
            }else{
                return $this->error('添加失败','teacher/insertTeacher');
            }

        }else{
            //查询出科目的名字
            $select_c_name = teacherModel::selectCourse();
            $this->assign('select_c_name',$select_c_name);
            return $this->fetch('teacher/insertTeacher');
        }
    }

    //---删除老师
    public function deleteTeacher()
    {
        if($this->request->isGet())
        {
            $deleteTeacher  = $this->request->get();
            $result = teacherModel::deleteTeacherModel($deleteTeacher);
            if($result)
            {
                return $this->success('删除成功','selectTeacher');
            }else{
                return $this->error('删除失败','selectTeacher');
            }
        }else{
            return $this->error('删除失败','selectTeacher');
        }
    }

    //---修改老师---需要先查询出老师原先状态在进行修改
    public function updateTeacher()
    {
        if($this->request->isPost())
        {
            $updateTeacher = $this->request->post();
            if(!(isset($updateTeacher['c_name'])))
            {
                //return $this->error('修改失败:必须添加一科','teacher/updateTeacher');
                return $this->error('修改失败','selectTeacher');
            }
            $result = teacherModel::updateTeacher($updateTeacher);
            if($result)
            {
                return $this->success('修改成功','selectTeacher');
            }else{
                return $this->error('修改失败','selectTeacher');
            }


        }else{

            $start = $this->request->get();
            //获取需要的一行数据，因为每次修改只用修改一行
            $updateTeacher = teacherModel::GetTeacherModel($start);
            //查出科目表现在目前的科目数量和科目id
            $select_c_name = teacherModel::selectCourse();
            //妈的，试了好多方法，这次没办法了，老子直接把师科表里面的提取出来就是了，看看老师选了多少科
            $t_id['t_id'] = $updateTeacher['id'];
            $selectTeaching = teacherModel::teachingModel($t_id);
            //抽取出师科表里面的的c_id出来，这样才能用 in_array()判断
            $selectTeaching_id = array_column($selectTeaching,'c_id');
            //var_dump($updateTeacher,$select_c_name,$selectTeaching_id);
            $this->assign('updateTeacher',$updateTeacher);
            $this->assign('select_c_name',$select_c_name);
            $this->assign('selectTeaching_id',$selectTeaching_id);
            return $this->fetch('updateTeacher');
        }



    }
}